Security News
Introducing the Socket Python SDK
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
@opentelemetry/instrumentation-aws-lambda
Advanced tools
OpenTelemetry AWS Lambda automatic instrumentation package.
@opentelemetry/instrumentation-aws-lambda is an npm package that provides automatic instrumentation for AWS Lambda functions using OpenTelemetry. It helps in collecting and exporting telemetry data such as traces and metrics, which can be used for monitoring and observability of serverless applications.
Automatic Tracing
This feature allows you to automatically trace AWS Lambda function invocations. The code sample demonstrates how to set up the NodeTracerProvider and register the AWS Lambda instrumentation.
const { NodeTracerProvider } = require('@opentelemetry/node');
const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
instrumentations: [
new AwsLambdaInstrumentation(),
],
});
exports.handler = async (event) => {
// Your Lambda function code
};
Custom Span Attributes
This feature allows you to add custom attributes to spans within your AWS Lambda function. The code sample shows how to retrieve the current span and set a custom attribute.
const { context, trace } = require('@opentelemetry/api');
exports.handler = async (event) => {
const span = trace.getSpan(context.active());
if (span) {
span.setAttribute('custom.attribute', 'value');
}
// Your Lambda function code
};
Error Handling
This feature allows you to record exceptions and set the status of spans when errors occur in your AWS Lambda function. The code sample demonstrates how to catch errors, record them, and set the span status.
const { context, trace } = require('@opentelemetry/api');
exports.handler = async (event) => {
const span = trace.getSpan(context.active());
try {
// Your Lambda function code
} catch (error) {
if (span) {
span.recordException(error);
span.setStatus({ code: 2, message: error.message });
}
throw error;
}
};
This package provides automatic instrumentation for HTTP and HTTPS modules in Node.js. It is similar to @opentelemetry/instrumentation-aws-lambda in that it helps in collecting telemetry data, but it is specifically designed for HTTP/HTTPS requests rather than AWS Lambda functions.
This package provides automatic instrumentation for Express.js applications. It is similar to @opentelemetry/instrumentation-aws-lambda in terms of providing observability, but it focuses on Express.js middleware and routes instead of AWS Lambda functions.
This package provides automatic instrumentation and monitoring for AWS Lambda functions using Datadog. It is similar to @opentelemetry/instrumentation-aws-lambda in that it helps in collecting telemetry data for Lambda functions, but it is specifically designed to work with Datadog's monitoring platform.
component owners: @carolabadeer
This module provides automatic instrumentation for the AWS Lambda
module, which may be loaded using the @opentelemetry/sdk-trace-node
package and is included in the @opentelemetry/auto-instrumentations-node
bundle.
If total installation size is not constrained, it is recommended to use the @opentelemetry/auto-instrumentations-node
bundle with @opentelemetry/sdk-node for the most seamless instrumentation experience.
This module is currently under active development and not ready for general use.
Compatible with OpenTelemetry JS API and SDK 1.0+
.
npm install --save @opentelemetry/instrumentation-aws-lambda
Create a file to initialize the instrumentation, such as lambda-wrapper.js
.
const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');
const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');
const { registerInstrumentations } = require('@opentelemetry/instrumentation');
const provider = new NodeTracerProvider();
provider.register();
registerInstrumentations({
instrumentations: [
new AwsLambdaInstrumentation({
// see under for available configuration
})
],
});
In your Lambda function configuration, add or update the NODE_OPTIONS
environment variable to require the wrapper, e.g.,
NODE_OPTIONS=--require lambda-wrapper
Options | Type | Description |
---|---|---|
requestHook | RequestHook (function) | Hook for adding custom attributes before lambda starts handling the request. Receives params: span, { event, context } |
responseHook | ResponseHook (function) | Hook for adding custom attributes before lambda returns the response. Receives params: span, { err?, res? } |
disableAwsContextPropagation | boolean | By default, this instrumentation will try to read the context from the _X_AMZN_TRACE_ID environment variable set by Lambda, set this to true or set the environment variable OTEL_LAMBDA_DISABLE_AWS_CONTEXT_PROPAGATION=true to disable this behavior |
eventContextExtractor | EventContextExtractor (function) | Function for providing custom context extractor in order to support different event types that are handled by AWS Lambda (e.g., SQS, CloudWatch, Kinesis, API Gateway). Applied only when disableAwsContextPropagation is set to true . Receives params: event, context |
lambdaHandler | string | By default, this instrumentation automatically determines the Lambda handler function to instrument. This option is used to override that behavior by explicitly specifying the Lambda handler to instrument. See Specifying the Lambda Handler for additional information. |
const { AwsLambdaInstrumentation } = require('@opentelemetry/instrumentation-aws-lambda');
new AwsLambdaInstrumentation({
requestHook: (span, { event, context }) => {
span.setAttribute('faas.name', context.functionName);
},
responseHook: (span, { err, res }) => {
if (err instanceof Error) span.setAttribute('faas.error', err.message);
if (res) span.setAttribute('faas.res', res);
}
})
The instrumentation will attempt to automatically determine the Lambda handler function to instrument. To do this, it relies on the _HANDLER
environment variable which is set by the Lambda runtime. For most use cases, this will accurately represent the handler that should be targeted by this instrumentation.
There exist use cases where the _HANDLER
environment variable does not accurately represent the module that should be targeted by this instrumentation. For these use cases, the lambdaHandler
option can be used to explicitly specify the Lambda handler that should be instrumented.
To better explain when lambdaHandler
should be specified, consider how some telemetry tools, such as Datadog, are instrumented into the Lambda runtime. Datadog does this by overriding the handler function with a wrapper function that is loaded via a Lambda Layer. In these examples, the Lambda's handler will point to the Datadog wrapper and not to the actual handler that should be instrumented. In cases like this, lambdaHandler
should be used to explicitly specify the handler that should be instrumented.
The lambdaHandler
should be specified as a string in the format <file>.<handler>
, where <file>
is the name of the file that contains the handler and <handler>
is the name of the handler function. For example, if the handler is defined in the file index.js
and the handler function is named handler
, the lambdaHandler
should be specified as index.handler
.
One way to determine if the lambdaHandler
option should be used is to check the handler defined on your Lambda. This can be done by determining the value of the _HANDLER
environment variable or by viewing the Runtime Settings of your Lambda in AWS Console. If the handler is what you expect, then the instrumentation should work without the lambdaHandler
option. If the handler points to something else, then the lambdaHandler
option should be used to explicitly specify the handler that should be instrumented.
Apache 2.0 - See LICENSE for more information.
FAQs
OpenTelemetry instrumentation for AWS Lambda function invocations
The npm package @opentelemetry/instrumentation-aws-lambda receives a total of 773,860 weekly downloads. As such, @opentelemetry/instrumentation-aws-lambda popularity was classified as popular.
We found that @opentelemetry/instrumentation-aws-lambda demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The initial version of the Socket Python SDK is now on PyPI, enabling developers to more easily interact with the Socket REST API in Python projects.
Security News
Floating dependency ranges in npm can introduce instability and security risks into your project by allowing unverified or incompatible versions to be installed automatically, leading to unpredictable behavior and potential conflicts.
Security News
A new Rust RFC proposes "Trusted Publishing" for Crates.io, introducing short-lived access tokens via OIDC to improve security and reduce risks associated with long-lived API tokens.